package com.csun.cmny.provider.service.impl;

import com.csun.cmny.base.dto.LoginAuthDto;
import com.csun.cmny.core.support.BaseService;
import com.csun.cmny.provider.dao.OmcOldOutVarMapper;
import com.csun.cmny.provider.dao.SessionMapper;
import com.csun.cmny.provider.model.constant.OldOutConstant;
import com.csun.cmny.provider.model.constant.ReviseConstant;
import com.csun.cmny.provider.model.domain.OmcOldOutVar;
import com.csun.cmny.provider.model.domain.OmcReviseVar;
import com.csun.cmny.provider.model.enums.UacRoleCodeEnum;
import com.csun.cmny.provider.service.OmcOldOutVarService;
import com.csun.cmny.util.PublicUtil;
import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @author chenzheng
 * @description:出院流程变量服务层
 * @date 2019/9/16 19:06
 */
@Slf4j
@Service
@Transactional
public class OmcOldOutVarServiceImpl extends BaseService<OmcOldOutVar> implements OmcOldOutVarService {

    @Resource
    private OmcOldOutVarMapper omcOldOutVarMapper;

    @Autowired
    private SessionMapper sessionMapper;

    /**
     * init oldOut var
     *
     * @param loginAuthDto  the login auth dto
     *
     * @return  the int
     */
    @Override
    public Integer init(LoginAuthDto loginAuthDto) {

        List<OmcOldOutVar> omcOldOutVarList = Lists.newArrayList();

        // service
        OmcOldOutVar service = new OmcOldOutVar();
        service.setGroupId(loginAuthDto.getGroupId());
        service.setUpdateInfo(loginAuthDto);
        service.setVarKey(OldOutConstant.OLD_OUT_VAR_SERVICE);
        service.setVarValue(UacRoleCodeEnum.SERVICE_MINISTER.getCode());

        // admit
        OmcOldOutVar admit = new OmcOldOutVar();
        admit.setGroupId(loginAuthDto.getGroupId());
        admit.setUpdateInfo(loginAuthDto);
        admit.setVarKey(OldOutConstant.OLD_OUT_VAR_ADMIT);
        List<String> codeList = Lists.newArrayList();
        codeList.add(UacRoleCodeEnum.RECEPTIONIST.getCode());
        codeList.add(UacRoleCodeEnum.RECEPTION_MINISTER.getCode());
        codeList.add(UacRoleCodeEnum.EVALUATE.getCode());
        admit.setVarValue(StringUtils.join(codeList.toArray(), ","));

        // charge
        OmcOldOutVar charge = new OmcOldOutVar();
        charge.setGroupId(loginAuthDto.getGroupId());
        charge.setUpdateInfo(loginAuthDto);
        charge.setVarKey(OldOutConstant.OLD_OUT_VAR_CHARGE);
        charge.setVarValue(UacRoleCodeEnum.CHARGE.getCode());

        // medical
        OmcOldOutVar medical = new OmcOldOutVar();
        medical.setGroupId(loginAuthDto.getGroupId());
        medical.setUpdateInfo(loginAuthDto);
        medical.setVarKey(OldOutConstant.OLD_OUT_VAR_MEDICAL);
        medical.setVarValue(UacRoleCodeEnum.MEDICAL_MINISTER.getCode());

        omcOldOutVarList.add(service);
        omcOldOutVarList.add(admit);
        omcOldOutVarList.add(charge);
        omcOldOutVarList.add(medical);


        return batchSave(omcOldOutVarList);
    }

    /**
     * destroy oldOut var
     *
     * @param loginAuthDto  the login auth dto
     *
     * @return  the int
     */
    @Override
    public Integer destroy(LoginAuthDto loginAuthDto) {

        OmcOldOutVar omcOldOutVar = new OmcOldOutVar();
        omcOldOutVar.setGroupId(loginAuthDto.getGroupId());

        return delete(omcOldOutVar);
    }

    /**
     * query oldOut var by group id and key
     *
     * @param groupId   the group id
     * @param key       the revise var key
     *
     * @return  the revise value by key
     */
    @Override
    public String queryVarByGroupIdAndKey(Long groupId, String key) {
        OmcOldOutVar omcOldOutVar = new OmcOldOutVar();
        omcOldOutVar.setVarKey(key);
        omcOldOutVar.setGroupId(groupId);

        omcOldOutVar = selectOne(omcOldOutVar);
        if (omcOldOutVar == null || PublicUtil.isEmpty(omcOldOutVar.getVarValue())) {
            return null;
        }

        return omcOldOutVar.getVarValue();
    }

    @Override
    public Map<String, Object> queryVarByGroupId(Long groupId) {

        return sessionMapper.queryOldOutVarByGroupId(groupId);
    }
}
